package com.baimifan.renovation.manager;

import android.os.Handler;
import android.os.Message;

import com.baimifan.renovation.MyApplication;
import com.baimifan.renovation.model.AddressData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by baimifan on 2017/07/06.
 */

public class AddressManager {
    private Handler mhandler;
    private String url = MyApplication.MAIN + "/index/index/province";
    private String url2 = MyApplication.MAIN + "/index/index/pro";
    private ArrayList<AddressData> list = new ArrayList<>();

    private ArrayList<AddressData> list2 = new ArrayList<>();

    public ArrayList<AddressData> getprovincelist () {
        return list;
    }

    public ArrayList<AddressData> getcitylist () {
        return list2;
    }

    public AddressManager (Handler handler) {
        this.mhandler = handler;
    }

    /**
     * 查市
     * @param id
     */
    public void provincelist (String id) {
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("id", id);
        x.http().post(params, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject result) {
                try {
                    list.clear();
                    if (result.getInt("status") == 200) {
                        JSONArray array = result.getJSONArray("data");
                        for (int i = 0; i<array.length(); i++) {
                            AddressData addressdata = new AddressData();
                            addressdata.setName(array.getString(i));
                            list.add(addressdata);
                        }
                        Message msg = new Message();
                        msg.what = 7;
                        mhandler.sendMessage(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 省份
     */
    public void citylist () {
        RequestParams params = new RequestParams(url2);
        x.http().post(params, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject result) {
                try {
                    if (result.getInt("status") == 200) {
                        list2.clear();
                        JSONArray array = result.getJSONArray("data");
                        for (int i = 0; i<array.length(); i++) {
                            AddressData addressdata = new AddressData();
                            JSONObject data = array.getJSONObject(i);
                            if (!data.isNull("p_name")) {
                                addressdata.setName(data.getString("p_name"));
                            }
                            if (!data.isNull("id")) {
                                addressdata.setId(data.getInt("id"));
                            }
                            list2.add(addressdata);
                        }
                        Message msg = new Message();
                        msg.what = 8;
                        mhandler.sendMessage(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
